package team1.project2.metrics;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;

import team1.project2.vmlogs.NormalisedConnection;

public class LogDAO {

	NormalisedConnection conn = new NormalisedConnection();
	
	public Vector<KeywordSearch> getKeywordOccurences(String s){
		Vector<KeywordSearch> ks = new Vector<KeywordSearch>();
		try {
			Connection con = conn.getConnection();
			String distDate = "select DISTINCT DATE(DATE_TIME) from logs.sys_logs";
//			String query = "SELECT DATE_TIME FROM logs.sys_logs where EVENT LIKE '%" + s +"%'";
			
			
			Statement stmt = null;
			ResultSet rs = null;
			
			stmt = con.createStatement();
			rs = stmt.executeQuery(distDate);
			
			int rowCount = 0;
			rs.last();
			rowCount = rs.getRow();
			rs.beforeFirst();
			String[] timeStamp = new String[rowCount];
			int i =0;
			while(rs.next()){
				timeStamp[i] = rs.getString(1).toString();
//				System.out.println("timeStamp[" + i + "]: " + timeStamp[i]);
				i++;
			}
			Connection con1 = conn.getConnection();
			Statement st = con1.createStatement();
			
			for(int j = 0; j < timeStamp.length; j++){
		
//				System.out.println("Timestamp[13]: " + timeStamp[13]);
				
				
				String query = "select DATE_TIME, count(*) from logs.sys_logs where EVENT like '" +
				"%" + s + "%' and DATE_TIME BETWEEN '"+ timeStamp[j] + " 00:00:00.000' " +
				"AND '" + timeStamp[j] + " 23:59:59.997'";
				
				System.out.println("Query: " + query);
				
//				String query = "select DATE_TIME, count(*) from logs.sys_logs where EVENT like '" +
//				"%" + s + "%' and DATE_TIME BETWEEN '2012-10-30 00:00:00.000' " +
//				"AND '2012-10-30 23:59:59.997'";
//		
				
				ResultSet resSet = st.executeQuery(query);
				resSet.last();
				rowCount = resSet.getRow();
				resSet.beforeFirst();
//				System.out.println("Last row: " + rowCount);
				
				if(rowCount > 0 && resSet != null){
					while(resSet.next() && resSet.getString(1) != null) {
						
						KeywordSearch kw = new KeywordSearch();
//						System.out.println("TimeStamp: " + resSet.getString(1).toString());
						kw.setTimeStamp(resSet.getString(1));
						kw.setNoOfOccurences(resSet.getInt(2));
						ks.add(kw);
					}
				}
				
				
			}
			st.close();
			con1.close();
			
			stmt.close();
			con.close();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ks;
		
	}
	
	public ArrayList<LineSearch> getLinesFromSysLogs(String s){
		ArrayList<LineSearch> ls = new ArrayList<LineSearch>();
		
		try {
			Connection con = conn.getConnection();
			String distDate = "select DATE_TIME, EVENT from logs.sys_logs WHERE EVENT LIKE '%" + s + "%'";
//			String query = "SELECT DATE_TIME FROM logs.sys_logs where EVENT LIKE '%" + s +"%'";
			
			
			Statement stmt = null;
			ResultSet rs = null;
			
			stmt = con.createStatement();
			rs = stmt.executeQuery(distDate);
			
			int rowCount = 0;
			rs.last();
			rowCount = rs.getRow();
			rs.beforeFirst();

			while(rs.next() && rs.getString(1) != null){
				LineSearch ll = new LineSearch();
				ll.setDate_time(rs.getString(1).toString());
				ll.setLine(rs.getString(2).toString());
				ls.add(ll);
			}
			
			stmt.close();
			con.close();
			return ls;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		
	}

	public ArrayList<VMKernelLogs> getFromKernelCPU(String cpu){
		ArrayList<VMKernelLogs> vmkernelLst = new ArrayList<VMKernelLogs>();
		
		try{
			Connection con = conn.getConnection();
			String query = "SELECT DATE_TIME, UPTIME_OF_HOST, CPU_OR_CORE_NAME, WID, CODE_REPORTING_MESSAGE, " +
					"LINE_OF_REPORTING_MESSAGE, MESSAGE_CONTENT FROM logs.kernel_logs where CPU_OR_CORE_NAME='" + cpu + "'";
			
			Statement stmt = null;
			ResultSet rs = null;
			
			stmt = con.createStatement();
			rs = stmt.executeQuery(query);
			
			while(rs.next() && rs.getString(1) != null){
				VMKernelLogs vm = new VMKernelLogs();
				vm.setCode_reporting_message(rs.getString(5).toString());
				vm.setDate_time(rs.getString(1).toString());
				vm.setHost_uptime(rs.getString(2).toString());
				vm.setCpuName(cpu);
				vm.setWid(rs.getString(4).toString());
				vm.setLineNum(rs.getString(6).toString());
				vm.setMessageContent(rs.getString(7).toString());
				vmkernelLst.add(vm);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return vmkernelLst;
	}
	
	public ArrayList<VMKernelLogs> getFromKernelWarningCPU(String cpu){
		ArrayList<VMKernelLogs> vmkernelLst = new ArrayList<VMKernelLogs>();
		
		try{
			Connection con = conn.getConnection();
			String query = "SELECT DATE_TIME, UPTIME_OF_HOST, CPU_OR_CORE_NAME, WID, CODE_REPORTING_MESSAGE, " +
					"LINE_OF_REPORTING_MESSAGE, MESSAGE_CONTENT FROM logs.kernel_warnings where CPU_OR_CORE_NAME='" + cpu + "'";
			
			Statement stmt = null;
			ResultSet rs = null;
			
			stmt = con.createStatement();
			rs = stmt.executeQuery(query);
			
			while(rs.next() && rs.getString(1) != null){
				VMKernelLogs vm = new VMKernelLogs();
				vm.setCode_reporting_message(rs.getString(5).toString());
				vm.setDate_time(rs.getString(1).toString());
				vm.setHost_uptime(rs.getString(2).toString());
				vm.setCpuName(cpu);
				vm.setWid(rs.getString(4).toString());
				vm.setLineNum(rs.getString(6).toString());
				vm.setMessageContent(rs.getString(7).toString());
				vmkernelLst.add(vm);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return vmkernelLst;
	}
	
	public static void main(String[] args){
		LogDAO ld = new LogDAO();
//		Vector<KeywordSearch> ks = ld.getKeywordOccurences("error");
//		System.out.println("ks size: " + ks.size());
//		for(int m = 0; m < ks.size(); m++){
//			System.out.println("timestamp: " + ks.get(m).getTimeStamp());
//			System.out.println("No. of occurences: " + ks.get(m).getNoOfOccurences());
//		}
		
		ArrayList<VMKernelLogs> lines = ld.getFromKernelCPU("cpu0");
		for(int i = 0; i < lines.size(); i++){
			System.out.println("lines: " + lines.get(i).getDate_time() + " : " + lines.get(i).getMessageContent());
		}
		
		
	}
}
